package com.yiliao.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.yiliao.service.ActivityService;
import com.yiliao.service.SentenceService;
import com.yiliao.util.DateUtils;
import com.yiliao.util.MessageUtil;

@Service
public class SentenceServiceImpl extends ICommServiceImpl implements SentenceService {

	@Override
	public JSONObject getSentenceList(int page, int size,int userId) {
		JSONObject jsonObject = new JSONObject();
		try {
			
			String qSql = "SELECT *  FROM t_send_sentence where t_user_id=?  order by t_sort asc ,t_id asc  LIMIT ?,10 ";
			List<Map<String, Object>> dataMap = this.getFinalDao().getIEntitySQLDAO().findBySQLTOMap(qSql,userId, (page-1)*10);
			String cSql = "SELECT count(1) AS totalCount FROM t_send_sentence where t_user_id=? ";
			Map<String, Object> totalMap = this.getFinalDao().getIEntitySQLDAO().findBySQLUniqueResultToMap(cSql,userId);
			
			jsonObject.put("total", totalMap.get("totalCount"));
			jsonObject.put("rows", dataMap);
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取活动列表异常!", e);
		}
		return jsonObject;
	}

	@Override
	public MessageUtil addSentence(Integer t_id, String t_user_id, String t_sentence_content, String t_sentence_type,
			int t_sentence_time, String t_sort,String t_sentence_param) {
		MessageUtil mu = null;
		try {
			//新增数据
			if(null == t_id || 0 == t_id){
				int t_anchor_type=1;
				List<Map<String,Object>> returnList = this.returnList("select t_user_id from t_virtual where t_user_id=?", t_user_id);
				if(returnList!=null && !returnList.isEmpty()) {
					t_anchor_type=0;
				}
				
//				if(t_anchor_type==1&&(t_sentence_type.equals("4")||t_sentence_type.equals("5"))) {
//					return mu = new MessageUtil(2, "真实主播无法添加虚拟视频/语音!");
//				}
				
				//语术主表
				List<Map<String,Object>> anchorSen = this.returnList("select t_sentence_id from t_user_sentence  where t_anchor_id=?", t_user_id);
				int t_sentence_id=0;
				if(anchorSen==null||anchorSen.isEmpty()) {
					String inSql = "INSERT INTO t_sentence (t_create_time) VALUES (now()) ";
					 t_sentence_id = this.getFinalDao().getIEntitySQLDAO().saveData(inSql);
					this.executeSql("insert into t_user_sentence (t_sentence_id,t_anchor_id,t_send_user_list,t_anchor_type) values (?,?,?,?)", t_sentence_id,t_user_id,"",t_anchor_type);
				}else {
					t_sentence_id=Integer.parseInt(anchorSen.get(0).get("t_sentence_id").toString());
				}
				
				String inSql = "INSERT INTO t_send_sentence (t_user_id, t_sentence_content,t_sentence_type,t_sentence_time, t_sort, t_create_time,t_sentence_id,t_sentence_param) VALUES (?,?,?,?,?,now(),?,?) ";
				this.getFinalDao().getIEntitySQLDAO().executeSQL(inSql,
						t_user_id,t_sentence_content,t_sentence_type,t_sentence_time,t_sort,t_sentence_id,t_sentence_param);
				mu = new MessageUtil(1, "添加活动成功!");
			}else{
				String uSql = "UPDATE t_send_sentence SET  t_sentence_content=?, t_sentence_type=?, t_sentence_time = ?,t_sort=? ,t_sentence_param=?  WHERE t_id=? ";
				this.getFinalDao().getIEntitySQLDAO().executeSQL(uSql,t_sentence_content,t_sentence_type,t_sentence_time,t_sort,t_sentence_param,t_id);
				
				mu = new MessageUtil(1, "更新完成!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("新增或者修改活动异常!", e);
			mu = new MessageUtil(0, "程序异常!");
		}
		return mu;
	}

	@Override
	public MessageUtil delSentence(int t_id) {
		MessageUtil mu = null;
		try {
				String sql="DELETE from t_send_sentence where t_id=? ";
				this.executeSql(sql, t_id);
				mu = new MessageUtil(1, "更新完成!");
			} catch (Exception e) {
				e.printStackTrace();
				mu = new MessageUtil(0, "程序异常!");
		}
		return mu;
	}

}
